package org.projectback.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
public class RabbitConfiguration {
    //创建死信交换机
    @Bean("exchange")
    public Exchange exchange() {
        return ExchangeBuilder.directExchange("exchange").build();
    }
    //创建死信队列 当别的队列有死信时 将添加到这个队列来进行处理
    @Bean("deadQueue")
    public Queue deadQueue() {
        return QueueBuilder.durable("deadQueue").build();
    }

    @Bean("Binding")
    public Binding binding(@Qualifier("deadQueue") Queue deadQueue,@Qualifier("exchange") Exchange exchange) {
        //将死信队列与交换机绑定在一起 并设定绑定在交换机中的Key
        return BindingBuilder.bind(deadQueue).to(exchange).with("deadQueue01").noargs();
    }
    //自定义了一个消息队列 使spring 使用自定义消息队列
    @Bean("emailQueue")
    public Queue emailQueue() {
        QueueBuilder queueBuilder = QueueBuilder.durable("email");
        //绑定死信交换机 当有死信时通知交换机发给对应的私信队列
        queueBuilder.deadLetterExchange("exchange");
        //死信发送给注册在交换机中的哪一个死信队列 使用Key
        queueBuilder.deadLetterRoutingKey("deadQueue01");
        return queueBuilder.build();
    }
    //自定义格式化器 使用JackSon2对数据进行格式化
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
